package com.pw.study.service;



/**
 * 顺序栈
 * @param <T> 泛型
 */
public class SequenceStack<T> {
    final int MaxSize = 10;
    private T[] stackArray;
    private int top;

    /*0.栈初始化*/
    public SequenceStack() {

        top = -1;
        stackArray = (T[]) new Object[MaxSize];
    }

    /*0-2自定义栈大小*/
    public SequenceStack(int n) {
        if (n <= 0) {
            System.out.println("数组长度要大于0，否则退出程序运行！");
            System.exit(1);
        }
        top = -1;
        stackArray = (T[]) new Object[n];
    }

    //1.在栈顶位置插入一个新元素
    public void push(T obj) {
        System.out.println(obj);
        if (top == stackArray.length - 1) {
            T[] p = (T[]) new Object[top * 2 + 2];
            for (int i = 0; i <= top; i++) {
                p[i] = stackArray[i];
            }
            stackArray = p;
        }
        System.out.println(obj instanceof Integer);
        top++;
        System.out.println(stackArray+" 22222" );
        int b=stackArray.length;
        System.out.println(b+" bbbbbb");
        stackArray[1] = (T) obj;

    }

    /*2.删除栈顶数据元素*/
    public T pop() {
        if (top == -1) {
            System.out.println("数据栈已空，无法删除元素");
            return null;
        }
        top--;
        return stackArray[top + 1];
    }

    /* 3.取出栈顶元素*/
    public T getHead() {
        if(top==-1){
            System.out.println("数据栈已空，无法删除元素");
            return null;
        }
        return stackArray[top];
    }

    /*4.判断当前栈是否为空*/
    public boolean isEmpty() {
        return top==-1;
    }

    /*5.栈中数据元素*/
    public int size() {
        return top+1;
    }

    /*6.依次访问栈中每个元素并输出*/
    public void nextOrder() {
        for (int i = 0; i >=0 ; i--) {
            System.out.println(stackArray[i]);
        }
    }

    /*7.销毁栈*/
    public void clear() {
        top=-1;
    }


}
